package gsd.gaudi.common.model.structure;

import gsd.gaudi.common.model.GaudiEventModel;
import gsd.gaudi.common.model.GaudiObject;

import java.awt.Component;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;

public class GaudiGuiStructure implements Serializable{

	private static final long serialVersionUID = 1L;

	private HashMap<String, GaudiObject> K2W;
	private HashMap<GaudiObject, String> W2K;
	
	private HashMap<String, String> clickToOpen;
	private HashMap<String, String> clickToClose;
	private HashMap<String, String> lister2widget;
	
	private LinkedList<GaudiObject> roots;
	
	public GaudiGuiStructure(){
		this.K2W = new HashMap<String, GaudiObject>();
		this.W2K = new HashMap<GaudiObject, String>();
		this.roots = new LinkedList<GaudiObject>();
		this.clickToOpen = new HashMap<String,String>();
		this.clickToClose = new HashMap<String,String>();
		this.lister2widget = new HashMap<String, String>();
	}
	
	public void addWidget(GaudiObject o, String id, String pid){
		
		GaudiObject parent = null;
		
		if(pid != null){
			parent = K2W.get(pid);
		}
		
		if(parent == null){
			this.roots.add(o);
		}else{
			o.setParent(parent);
			parent.addChildren(o);
		}
		
		for(GaudiEventModel m : o.getEventList()){
			for(String l : m.getListeners()){
				this.lister2widget.put(l, o.getId());
			}
		}
		
		this.K2W.put(id, o);
		this.W2K.put(o, id);
		
	}
	
	public void addOpenWindowEvent(String id, String opened){
		this.clickToOpen.put(opened, id);
	}
	
	public void addCloseWindowEvent(String id, String closed){
		this.clickToClose.put(closed, id);
	}
	
	public String findWidgetId(Component w){
		String id = this.W2K.get(w);
		return id;
	}
	
	public GaudiObject findWidget(String id){
		GaudiObject w = this.K2W.get(id);
		return w;
	}
	
	public LinkedList<GaudiObject> getRoots(){
		return this.roots;
	}
	
	public HashMap<String, String> getClickToOpen() {
		return clickToOpen;
	}

	public void setClickToOpen(HashMap<String, String> clickToOpen) {
		this.clickToOpen = clickToOpen;
	}

	public HashMap<String, String> getLister2widget() {
		return lister2widget;
	}

	public void setLister2widget(HashMap<String, String> lister2widget) {
		this.lister2widget = lister2widget;
	}
	
	public HashMap<String, GaudiObject> getK2W() {
		return K2W;
	}

	public void setK2W(HashMap<String, GaudiObject> k2w) {
		K2W = k2w;
	}

	public void printMe(){
		for(GaudiObject o : this.roots){
			o.printMe(0);
		}
	}
	
}
